#!/usr/bin/env perl

use strict;

if (scalar(@ARGV) != 2) {
    print "Usage: extract-transcript-to-gene-map-from-trinity trinity_fasta_file map_file\n";
    exit(-1);
}

open(INPUT, $ARGV[0]);
open(OUTPUT, ">$ARGV[1]");

my ($tag, $line);
$tag = <INPUT>; chomp($tag);
while (substr($tag, 0, 1) eq ">") {
    $tag = substr($tag, 1);
    my $cnt = 0;
    while (($line = <INPUT>) && substr($line, 0, 1) ne ">") {
	$cnt++;
    }
    if ($cnt == 0) { print "Warning: Fasta entry $tag has an empty sequence, it is omitted.\n"; }
    else {
	my ($tid, @tmp) = split(/ /, $tag);
	my $pos = rindex($tid, "_");
	my $gid = "";
	if ($pos >= 0) { $gid = substr($tid, 0, $pos); }
	else { $gid = $tid; }
	print OUTPUT "$gid\t$tid\n";
    }
    $tag = $line; chomp($tag);
} 

close(INPUT);
close(OUTPUT);
